package com.ahut.service.impl;

import com.ahut.utils.JwtHelper;
import com.ahut.utils.MD5Util;
import com.ahut.utils.Result;
import com.ahut.utils.ResultCodeEnum;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ahut.pojo.User;
import com.ahut.service.UserService;
import com.ahut.mapper.UserMapper;
import io.jsonwebtoken.Jwt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author zhuang05
* @description 针对表【news_user】的数据库操作Service实现
* @createDate 2025-03-07 20:51:41
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtHelper jwtHelper;

    /**
     * 检查用户名是否重复
     * @param username
     * @return
     */
    @Override
    public Result checkUserName(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user = userMapper.selectOne(queryWrapper);
        if (user != null){
            return Result.build(null,ResultCodeEnum.USERNAME_USED);
        }
        return Result.ok(null);
    }

    /**
     * 用户登录功能
     * @param user
     * @return
     */
    @Override
    public Result login(User user) {
        //需要知道账号和密码是否有误
        //先查询是否存在该用户，再进行密码查询
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userName", user.getUsername());
        User selectUser = userMapper.selectOne(queryWrapper);

        if (selectUser == null){
            //说明没找到， 该用户不存在
            return Result.build(null, ResultCodeEnum.USERNAME_ERROR);
        }
        if (!MD5Util.encrypt(user.getUserPwd()).equals(selectUser.getUserPwd())){
            //查到的用户密码与输入的密码不相同
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }
        String token = jwtHelper.createToken(selectUser.getUid());
        Map data = new HashMap<>();
        data.put("token",token);
        return Result.ok(data);
    }


    /**
     *
     * 1.判断token是否为空
     * 2.根据token解析用户id
     * 3.查询用户信息并将用户密码置空
     * @param token
     * @return
     */
    @Override
    public Result getUserInfo(String token) {
        boolean expiration = jwtHelper.isExpiration(token);
        if (expiration){
            //说明token已经过期
            return Result.build(null,ResultCodeEnum.NOTLOGIN);
        }

        //解析token获得用户id
        Long userId = jwtHelper.getUserId(token);

        //查询用户信息, 将用户密码置空
        User user = userMapper.selectById(userId);
        user.setUserPwd("");

        //返回数据
        Map data = new HashMap<>();
        data.put("loginUser",user);
        return Result.ok(data);
    }

    /**
     * 用户注册功能
     * 1.检查用户是否已经存在
     * 2.不存在则将密码进行加密并保存
     * @param user
     * @return
     */
    @Override
    public Result regist(User user) {

        if(checkUserName(user.getUsername()).getCode() == 505){
            //用户名已存在
            return Result.build(null,ResultCodeEnum.USERNAME_USED);
        }
        //将用户密码通过MD5加密存入数据库
        user.setUserPwd(MD5Util.encrypt(user.getUserPwd()));
        int rows = userMapper.insert(user);
        return Result.ok(null);
    }
}




